<!-- admin-dashboard/src/views/Login.vue -->
<template>
  <div class="login-container">
    <el-form :model="form" label-width="120px" @submit.prevent="handleLogin">
      <h2>Admin Login</h2>
      <el-form-item label="Username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="Password">
        <el-input v-model="form.password" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">Login</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';
import { ElMessage } from 'element-plus';

const form = ref({ username: '', password: '' });
const router = useRouter();

const handleLogin = async () => {
  try {
    const res = await axios.post('http://61.184.21.191:3000/api/v1/auth/login', form.value);
    localStorage.setItem('token', res.data.token);
    router.push('/dashboard');
    ElMessage.success('Login successful');
  } catch (err) {
    ElMessage.error('Login failed: ' + (err.response?.data?.error || err.message));
  }
};
</script>

<style scoped>
.login-container {
  max-width: 400px;
  margin: 100px auto;
}
</style>